package GUI.Productos;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Label;

import article.entities.ComboVO;
import article.exceptions.BadArticleException;
import article.exceptions.MissingArticleException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import dbExceptions.DataBaseErrorException;

public class BajaCombo extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	Label error;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public BajaCombo(Shell parent, int style) {
		super(parent, style);
		setText("Eliminar Combo");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(571, 302);
		shell.setText(getText());
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(23, 40, 300, 222);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText("Nombre Combo");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(201);
		tblclmnNewColumn_1.setText("Articulos Incluidos");
		
		Button btnEliminarDelSistema = new Button(shell, SWT.NONE);
		btnEliminarDelSistema.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(table.getSelection()[0]==null){
					error.setText("Seleccione un combo para eliminar");
				}
				else{
					TableItem select = table.getSelection()[0];
					ComboVO [] arts= null;
					ArticleProxyMgt o = ServiceFacade.getInstance().getArticleProxyMgt();
					try {
						arts= o.getCombos();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
						for (int j=0;j<arts.length;j++){
							if(arts[j].getName().equalsIgnoreCase(select.getText(0))){
								try {
									o.deleteCombo(arts[j]);
									table.remove(table.getSelectionIndex());
								} catch (RemoteException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								} catch (MissingArticleException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								} catch (BadArticleException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								} catch (DataBaseErrorException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								} catch (ProxyException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
								
								
							}
						}
						
					}
				
			}
		});
		btnEliminarDelSistema.setBounds(387, 137, 140, 28);
		btnEliminarDelSistema.setText("Eliminar del Menu");
		
		Button btnVolver = new Button(shell, SWT.NONE);
		btnVolver.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}
		});
		btnVolver.setBounds(387, 190, 140, 28);
		btnVolver.setText("Volver");
		
		error = new Label(shell, SWT.NONE);
		error.setBounds(341, 40, 220, 14);
		error.setText("");
		ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
		ComboVO[] ings=null;
		try {
			ings = uso.getCombos();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProxyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i=0;i<ings.length;i++){
			String aa = "";
			for(int j= 0; j < ings[i].getArts().size();j++){
				aa=aa + " " + ings[i].getArts().get(j).getName();
			}
			TableItem tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(0,ings[i].getName());
			tableItem.setText(1,aa);
		}

	}

	
}
